package com.atguigu.study.controller;

import com.atguigu.study.domain.User;
import com.atguigu.study.resp.ResultData;
import com.atguigu.study.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/add")
    public ResultData<Boolean> add(@RequestBody User user) {
        return ResultData.success(userService.save(user));
    }

    @DeleteMapping("/del/{id}")
    public boolean del(@PathVariable("id") Integer id) {
        return userService.removeById(id);
    }

    @PutMapping("/updateConcurrent")
    public boolean updateCouncurrent(@RequestBody User user) {
        User oldUser1 = userService.getBaseMapper().selectById(user.getId());
        User oldUser2 = userService.getBaseMapper().selectById(user.getId());

        oldUser1.setPassword("111111");
        userService.updateById(oldUser1);

        oldUser2.setPassword("222222");
        boolean b = userService.updateById(oldUser2);
        if (b) {
            log.info("更新成功");
            System.out.println("更新成功");
            return true;
        } else {
            log.info("更新失败");
            System.out.println("更新失败");
            return false;
        }
    }

    @GetMapping("/{id}")
    public ResultData<User> get(@PathVariable("id") Integer id) {
        return ResultData.success(userService.getById(id));
    }

    @GetMapping("/list")
    public List<User> list() {
        return userService.list();
    }

    @GetMapping("/get")
    public User get(@RequestParam("username") String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        return userService.getOne(wrapper);
    }

    @PutMapping("/update")
    public boolean update(@RequestBody User user) {
        User oldUser = userService.getBaseMapper().selectById(user.getId());
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(User::getPassword, user.getUsername())
                .set(User::getPassword, user.getPassword())
                .set(User::getSex, user.getSex())
                .set(User::getCreateTime, user.getCreateTime())
                .set(User::getVersion, user.getVersion())
                .eq(User::getId, user.getId())
                .eq(User::getVersion, oldUser.getVersion());
        return userService.update(wrapper);
    }

    @PutMapping("/selectPage")
    public List<User> selectPage(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        return userService.page(page).getRecords();
    }
}
